package com.chat.service;

import com.chat.Local;
import com.chat.common.ChatMsg;
import com.chat.common.Message;
import com.chat.common.MessageType;
import com.chat.io.SetFile;
import com.chat.view.ChatListData;
import com.chat.view.MainWindowView;
import javafx.application.Platform;

import java.util.ArrayList;
import java.util.Objects;

import static com.chat.view.MainWindowController.chat;

/*
   获取历史消息
 */
public class GetHistoryMsg {
        public void gethistorymsg(String sender,String receive){
                Message msg=new Message();
                msg.setMesType(MessageType.MESSAGE_GET_HISTORY_MSG);
                ChatMsg cm=new ChatMsg();
                cm.setSenderid(sender);
                cm.setReceive(receive);
                msg.setData(cm);
                ClientConnectServerThread clientConnectServerThread=ManageClientConnectServerThread.getClientConnectServerThread(Local.user.getUsername());
                clientConnectServerThread.send(msg);
        }
        public void sethistorymsg(ArrayList<ChatMsg> a){
                Platform.runLater(()-> {
                           chat.clear();
                          int k=a.size()-1;
                        for(int i=a.size()-1;i>=0;i--)
                        {       //自己发的文字和表情包
                                if(a.get(i).getSenderid().equals(Local.user.getUsername())&& Objects.equals(a.get(i).getType(), "1")){
                                        chat.add(new ChatListData(Local.user.getNickname(),a.get(i).getContent(),a.get(i).getSenderid(),a.get(i).getType(),a.get(i).getTime()));
                                }
                                //自己发的图片
                                else if(a.get(i).getSenderid().equals(Local.user.getUsername())&& Objects.equals(a.get(i).getType(), "2")){
                                        SetFile sf=new SetFile();
                                        sf.set((byte[]) a.get(i).getFile(),a.get(i).getFilename());//将图片下载到本地
                                        chat.add(new ChatListData(Local.user.getNickname(),a.get(i).getFilename(),a.get(i).getSenderid(),a.get(i).getType(),a.get(i).getTime()));
                                }
                                //自己发的文件
                                else if(a.get(i).getSenderid().equals(Local.user.getUsername())&& Objects.equals(a.get(i).getType(), "3")) {
                                        chat.add(new ChatListData(Local.user.getNickname(), a.get(i).getAddress(), a.get(i).getFilename(), a.get(i).getSenderid(), a.get(i).getTime(), a.get(i).getType()));
                                }
                                //对方发的文字和表情
                                else if(!Objects.equals(a.get(i).getSenderid(), Local.user.getUsername())&& Objects.equals(a.get(i).getType(), "1")) {
                                        chat.add(new ChatListData(Local.receivenickname, a.get(i).getContent(), a.get(i).getSenderid(), a.get(i).getType(),a.get(i).getTime()));
                                }
                                //对方发的图片
                                else if(!Objects.equals(a.get(i).getSenderid(), Local.user.getUsername())&& Objects.equals(a.get(i).getType(), "2")){
                                        SetFile sf=new SetFile();
                                        sf.set((byte[]) a.get(i).getFile(),a.get(i).getFilename());//将图片下载到本地
                                        chat.add(new ChatListData(Local.receivenickname,a.get(i).getFilename(),a.get(i).getSenderid(),a.get(i).getType(),a.get(i).getTime()));
                                }
                                //对方发的文件
                                else if(!Objects.equals(a.get(i).getSenderid(), Local.user.getUsername())&& Objects.equals(a.get(i).getType(), "3")){
                                        chat.add(new ChatListData(Local.receivenickname,a.get(i).getAddress(),a.get(i).getFilename(),a.get(i).getSenderid(),a.get(i).getTime(),a.get(i).getType()));
                                }

                        }
                        Platform.runLater(()-> {
                                ((javafx.scene.control.ListView) MainWindowView.$("chatlist")).setItems(chat);
                        });
                        Platform.runLater(()-> {
                                ((javafx.scene.control.ListView) MainWindowView.$("chatlist")).scrollTo(1000);
                        });
                });

        }
}
